package com.woniu.controller;

import com.woniu.entity.RbacManager;
import com.woniu.service.RbacManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author:陈涵
 * @Do:用户管理的Controller控制层
 * 前端访问控制层，控制层访问service，service访问dao，dao访问数据库
 *  @date: 2022/8/25 15:34
 */

//@Controller相当于day05讲过的@service,注册到Spring容器之中
@Controller
//相当于applicationContext.xml里面的<bean id="/myFirstController" class="com.woniu.controller.MyFirstContrller"/>
//相当于Http://localhost8080/rbacManager 这个请求就可以找到我的controller!!!!!
@RequestMapping("/rbacManager")
public class RbacManagerController {
    //自动注入
//没错啊
    @Autowired
    RbacManagerService rbacManagerService;

    //相当于请求Http://localhost8080/rbacManager/findAll
    @RequestMapping("/findAll")
    public String findAll(Model model, ModelMap modelMap, ModelAndView modelAndView, HttpServletRequest request, HttpServletResponse response) {
        List<RbacManager> managers = rbacManagerService.findAll();

//        request.setAttribute("data", managers);
//        request.getRequestDispatcher("WEB-INF/jsp/success.jsp");
        //上面2阶段的代码相当于下面这两行代码
        model.addAttribute("data", managers);
        return "success";
    }

    //Http://localhost8080/rbacManager/findByName
    @RequestMapping("/findByName")
    /*@RequestParam注解是为了避免前后端的取名不统一，比如前端用的是name，后端用的是username，这时候要求后端必须改名的解决方案
    加注解之前的访问路径是：http://localhost:8080/rbacManager/findByName?username=admin
    加注解之后的访问路径是：http://localhost:8080/rbacManager/findByName?name=admin
    不可以再通过http://localhost:8080/rbacManager/findByName?username=admin访问，会报错400
    * */

    public String findByName(@RequestParam("name") String username, Model model){
        List<RbacManager> list = rbacManagerService.findByName(username);
        model.addAttribute("data",list);
        return "success";
    }

    //method的取值表示限制请求是post请求，@PostMapping也行
    @RequestMapping(value = "/findByIds",method = RequestMethod.POST)
    public  String findByIds(Integer[] ids,Model model){
        List<RbacManager> byIds = rbacManagerService.findByIds(ids);
        model.addAttribute("data",byIds);
        return "success";
    }

    @RequestMapping(value = "/findByLists",method = RequestMethod.POST)
    public  String findByIds(@RequestParam List<Integer> ids,Model model){
        //????new Integer[0]
        Integer[] objects =  ids.toArray(new Integer[0]);
        List<RbacManager> list = rbacManagerService.findByIds(objects);
        model.addAttribute("data",list);
        return "success";
    }
    @RequestMapping("/addOneForMap")
    public String addOneForMap(@RequestParam Map<String, Object>userinfo,Model model){
        int i=rbacManagerService.addOneForMap(userinfo);
        model.addAttribute("data","新增用户"+userinfo);
        return "success";
    }

    @RequestMapping("/addOne")
    public String addOne(RbacManager rbacManager,Model model){
        int i=rbacManagerService.addOne(rbacManager);
        model.addAttribute("data",rbacManager);
        return "success";
    }
    
    
}
